var poolModule = require('generic-pool')
var mysql = require('mysql')

var mysqlConfig = {
	host: "127.0.0.1",
	port: "3306",
	database: "pro",
	user: "root",
	password: "123456"
}

//创建数据库连接池
var pool = poolModule.Pool({
	name: 'mysql',
	create: function(callback) {
		var client = mysql.createConnection({
			host: mysqlConfig.host,
			user: mysqlConfig.user,
			password: mysqlConfig.password,
			database: mysqlConfig.database
		});
		callback(null, client)
	},
	destroy: function(client) {
		client.end()
	},
	max: 100,
	min: 5,
	idleTimeoutMillis: 30000,
	log: false
})


var query = function(sql, args, cb) {
	pool.acquire(function(err, client) {
		if (!!err) {
			console.error('[sqlqueryErr]' + err.stack);
			return
		}
		client.query(sql, args, function(err, res) {
			pool.release(client)
			cb(err, res)
		})
	})
}
